package com.xxr.mom.ci.charging.interworking.v2016.accepter.query;

import cn.hutool.core.util.RandomUtil;
import com.xxr.mom.ci.charging.interworking.base.InterfaceAccepter;
import com.xxr.mom.ci.charging.interworking.dto.InterworkingConfigDto;
import com.xxr.mom.ci.charging.interworking.entity.InterworkingConfig;
import com.xxr.mom.ci.charging.interworking.v2016.AbstractV2016Service;
import com.xxr.mom.ci.charging.interworking.vo.api.AccessTokenRequest;
import com.xxr.mom.ci.charging.interworking.vo.api.AccessTokenResponse;
import com.xxr.mom.ci.common.dto.InterworkingParam;
import com.xxr.mom.ci.common.enums.InterworkingRetEnum;
import com.xxr.mom.ci.common.exception.InterworkingException;
import com.xxr.mom.ci.common.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 分布式认证的认证接口规范
 * 接口名称∶ query_token
 * 概述:此接口用于平台之间认证 Token的申请，Token作为全局唯一凭证，调用各接口时均需要使用。
 */
@Service
public class QueryTokenAccepterService extends AbstractV2016Service<AccessTokenRequest, AccessTokenResponse> implements InterfaceAccepter<AccessTokenRequest, AccessTokenResponse> {

    /**
     * token过期时间
     * 默认一天
     */
    @Value("${charging.interworking.tokenAvailableTime: 86400}")
    private Long tokenAvailableTime;

    @Override
    public void validateRequestParam(AccessTokenRequest accessTokenRequest) {
        if (StringUtils.isBlank(accessTokenRequest.getOperatorID()) || StringUtils.isBlank(accessTokenRequest.getOperatorSecret())) {
            throw new InterworkingException(InterworkingRetEnum.PARAMETER_ERROR);
        }
    }

    @Override
    public AccessTokenResponse dataHandler(AccessTokenRequest accessTokenRequest, InterworkingConfigDto configDto) {
        InterworkingConfig interworkingConfig = configDto.getInterworkingConfig();
        String operatorId = accessTokenRequest.getOperatorID();
        String operatorSecret = accessTokenRequest.getOperatorSecret();

        int sucStat = 1, failStat = 2;
        String accessToken = null;
        if (Objects.equals(operatorId, interworkingConfig.getOperatorId()) && Objects.equals(operatorSecret, interworkingConfig.getOperatorSecret())) {
            sucStat = 0;
            failStat = 0;
            String redisKey = Constant.RedisKey.REDIS_TOKEN_ACCEPT + operatorId;
            accessToken = RandomUtil.randomStringUpper(16);
            tokenRedisTemplate.opsForValue().set(redisKey, accessToken, tokenAvailableTime, TimeUnit.SECONDS);
        }

        AccessTokenResponse result = new AccessTokenResponse();
        result.setOperatorID(operatorId);
        result.setSucStat(sucStat);
        result.setAccessToken(accessToken);
        result.setTokenAvailableTime(tokenAvailableTime);
        result.setFailStat(failStat);
        return result;
    }

    /**
     * 不校验token
     *
     * @param token             token
     * @param interworkingParam param
     * @param configDto         config
     */
    @Override
    public void validateToken(String token, InterworkingParam interworkingParam, InterworkingConfigDto configDto) {
    }
}
